
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第三部分：使用中间码 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="part2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">一：第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../lab1/part0-intro.html">
            
                <a href="../lab1/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../lab1/part1-lex.html">
            
                <a href="../lab1/part1-lex.html">
            
                    
                    词法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../lab1/part1-1-task.html">
            
                <a href="../lab1/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../lab1/part2-parse.html">
            
                <a href="../lab1/part2-parse.html">
            
                    
                    语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../lab1/part2-1-rdp.html">
            
                <a href="../lab1/part2-1-rdp.html">
            
                    
                    递归下降分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../lab1/part2-2-task.html">
            
                <a href="../lab1/part2-2-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../lab1/part3-ir.html">
            
                <a href="../lab1/part3-ir.html">
            
                    
                    中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../lab1/part3-1-task.html">
            
                <a href="../lab1/part3-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.9" data-path="../lab1/part4-codegen.html">
            
                <a href="../lab1/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.10" data-path="../lab1/part4-1-task.html">
            
                <a href="../lab1/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.11" data-path="../lab1/summary.html">
            
                <a href="../lab1/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    [dzy] 仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    第一部分：从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    第二部分：词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.2.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    第三部分：使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.4.1" data-path="antlr.html">
            
                <a href="antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4.2" data-path="visitor.html">
            
                <a href="visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">二：常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    步骤二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/part0-intro.html">
            
                <a href="../lab2/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/summary.html">
            
                <a href="../lab2/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    步骤三
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/part0-intro.html">
            
                <a href="../lab3/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/summary.html">
            
                <a href="../lab3/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    步骤四
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/part0-intro.html">
            
                <a href="../lab4/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/summary.html">
            
                <a href="../lab4/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">三：变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    步骤五
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../lab5/part0-intro.html">
            
                <a href="../lab5/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../lab5/stackframe.html">
            
                <a href="../lab5/stackframe.html">
            
                    
                    栈帧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="../lab5/summary.html">
            
                <a href="../lab5/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    步骤六
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/part0-intro.html">
            
                <a href="../lab6/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/summary.html">
            
                <a href="../lab6/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">四：块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    步骤七
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/part0-intro.html">
            
                <a href="../lab7/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/namer.html">
            
                <a href="../lab7/namer.html">
            
                    
                    名称解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/summary.html">
            
                <a href="../lab7/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    步骤八
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/part0-intro.html">
            
                <a href="../lab8/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/part4-codegen.html">
            
                <a href="../lab8/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/part4-1-task.html">
            
                <a href="../lab8/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.4" data-path="../lab8/summary.html">
            
                <a href="../lab8/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">五：函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    步骤九
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../lab9/part0-intro.html">
            
                <a href="../lab9/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../lab9/part4-1-cconv.html">
            
                <a href="../lab9/part4-1-cconv.html">
            
                    
                    调用约定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../lab9/part4-2-codegen.html">
            
                <a href="../lab9/part4-2-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../lab9/part4-3-task.html">
            
                <a href="../lab9/part4-3-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../lab9/summary.html">
            
                <a href="../lab9/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    步骤十
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">六：指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    步骤十一
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    步骤十二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >第三部分：使用中间码</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;-step1&#xFF1A;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x7801;">&#x5B9E;&#x9A8C;&#x6307;&#x5BFC; step1&#xFF1A;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x7801;</h1>
<p>&#x6211;&#x4EEC;&#x7EE7;&#x7EED;&#x6539;&#x8FDB;&#x4E0A;&#x4E00;&#x6B65;&#x6211;&#x4EEC;&#x5F97;&#x5230;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x8FD9;&#x6B21;&#x8981;&#x505A;&#x7684;&#x662F;&#xFF1A; <strong>&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x7801;&#x8BA9;&#x7F16;&#x8BD1;&#x5668;&#x66F4;&#x6A21;&#x5757;&#x5316;</strong>&#x3002;</p>
<h2 id="&#x6808;&#x673A;&#x5668;&#x548C;&#x4E2D;&#x95F4;&#x8868;&#x793A;">&#x6808;&#x673A;&#x5668;&#x548C;&#x4E2D;&#x95F4;&#x8868;&#x793A;</h2>
<pre><code>        &#x8BCD;&#x6CD5;&#x5206;&#x6790;           &#x8BED;&#x6CD5;&#x5206;&#x6790;           *IR&#x751F;&#x6210;*      *&#x76EE;&#x6807;&#x4EE3;&#x7801;&#x751F;&#x6210;*
&#x5B57;&#x8282;&#x6D41; ----------&gt; Tokens ----------&gt; &#x8BED;&#x6CD5;&#x6811; ---------&gt; IR ---------------&gt; RISC-V &#x6C47;&#x7F16;
</code></pre><p>&#x53C2;&#x89C1; lab0/ir&#xFF08;&#x7B49;&#x5F85;&#x5B8C;&#x5584;&#xFF09;&#x3002;</p>
<p>&#x5BB9;&#x6613;&#x770B;&#x51FA;&#xFF0C;IR &#x7684;&#x597D;&#x5904;&#x6709;&#x5982;&#x4E0B;&#x51E0;&#x70B9;</p>
<ul>
<li>&#x7F29;&#x5C0F;&#x8C03;&#x8BD5;&#x8303;&#x56F4;&#xFF0C;&#x901A;&#x8FC7;&#x628A; AST &#x5230;&#x6C47;&#x7F16;&#x7684;&#x6B65;&#x9AA4;&#x4E00;&#x5206;&#x4E3A;&#x4E8C;&#x3002;<blockquote>
<p>&#x901A;&#x8FC7;&#x89C2;&#x5BDF; IR &#x662F;&#x5426;&#x6B63;&#x786E;&#x751F;&#x6210;&#x5C31;&#x80FD;&#x77E5;&#x9053;&#xFF1A;&#x5230;&#x5E95;&#x662F; IR &#x751F;&#x6210;&#x8FD9;&#x4E00;&#x5C0F;&#x6B65;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x8FD8;&#x662F; IR &#x5230;&#x6C47;&#x7F16;&#x8FD9;&#x4E00;&#x5C0F;&#x6B65;&#x6709;&#x95EE;&#x9898;&#x3002;
&#x6BD4;&#x8D77; AST &#x5230;&#x6C47;&#x7F16;&#x5F53;&#x6210;&#x4E00;&#x6574;&#x4E2A;&#x5927;&#x6B65;&#x9AA4;&#xFF0C;&#x5206;&#x6210;&#x4E24;&#x4E2A;&#x5C0F;&#x6B65;&#xFF0C;&#x6BCF;&#x6B65;&#x4EE3;&#x7801;&#x66F4;&#x5C11;&#xFF0C;&#x66F4;&#x5BB9;&#x6613;&#x8C03;&#x8BD5;&#x3002;</p>
</blockquote>
</li>
<li>&#x66F4;&#x5BB9;&#x6613;&#x9002;&#x914D;&#x4E0D;&#x540C;&#x6307;&#x4EE4;&#x96C6;&#xFF08;RISC-V, x86, MIPS, ARM...&#xFF09;&#x548C;&#x6E90;&#x8BED;&#x8A00;&#xFF08;MiniDecaf, Decaf, C, Java...&#xFF09;&#x3002;<blockquote>
<p>&#x4E0D;&#x540C;&#x6E90;&#x8BED;&#x8A00;&#x7684; AST &#x4E0D;&#x540C;&#xFF0C;&#x76F4;&#x63A5;&#x4ECE; AST &#x751F;&#x6210;&#x6C47;&#x7F16;&#x7684;&#x8BDD;&#xFF0C;&#x4E3A;&#x4E86;&#x652F;&#x6301; N &#x4E2A;&#x6E90;&#x8BED;&#x8A00;&#x548C; M &#x4E2A;&#x76EE;&#x6807;&#x6307;&#x4EE4;&#x96C6;&#xFF0C;&#x9700;&#x8981;&#x5199; N * M &#x4E2A;&#x76EE;&#x6807;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x6A21;&#x5757;&#xFF1A;</p>
<p><img src="pics/without-ir.svg" alt=""></p>
<p>&#x5982;&#x679C;&#x6709;&#x4E86; IR&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5199; N &#x4E2A; IR &#x751F;&#x6210;&#x548C; M &#x4E2A;&#x6C47;&#x7F16;&#x751F;&#x6210;&#xFF0C;&#x4E00;&#x5171; N + M &#x4E2A;&#x6A21;&#x5757;&#xFF1A;</p>
<p><img src="pics/with-ir.svg" alt=""></p>
</blockquote>
</li>
<li>&#x66F4;&#x5BB9;&#x6613;&#x6DFB;&#x52A0;&#x4E0D;&#x540C;&#x6E90;&#x8BED;&#x8A00;&#x3002;<blockquote>
<p>&#x5047;&#x8BBE;&#x540C;&#x6837;&#x7684; IR</p>
</blockquote>
</li>
</ul>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684; IR &#x662F;&#x6808;&#x5F0F;&#x673A; IR&#xFF0C;&#x5B83;&#x751F;&#x6210;&#x5F88;&#x7B80;&#x5355;&#x3001;&#x7FFB;&#x8BD1;&#x5230;&#x6C47;&#x7F16;&#x4E5F;&#x5F88;&#x7B80;&#x5355;&#x3002;</p>
<blockquote>
<p>&#x5F53;&#x7136;&#xFF0C;&#x5C31;&#x8BFE;&#x7A0B;&#x5B9E;&#x9A8C;&#x6765;&#x8BF4;&#xFF0C;&#x4F60;&#x4E0D;&#x4E00;&#x5B9A;&#x975E;&#x8981;&#x663E;&#x5F0F;&#x5730;&#x751F;&#x6210; IR&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4ECE; AST &#x751F;&#x6210;&#x6C47;&#x7F16;&#x3002;
&#x4F46;&#x53EA;&#x8981;&#x6309;&#x7167;&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;&#x4E66;&#x7684;&#x601D;&#x8DEF;&#xFF0C;&#x4F60;&#x4E00;&#x5B9A;&#x4F1A;&#x4F7F;&#x7528;&#x6808;&#x673A;&#x5668;&#x7684;&#x601D;&#x8DEF;&#x601D;&#x8003;&#xFF0C;
&#x54EA;&#x6015;&#x76F4;&#x63A5;&#x4ECE; AST &#x751F;&#x6210;&#x6C47;&#x7F16;&#xFF0C;&#x4E5F;&#x672C;&#x8D28;&#x4E0A;&#x662F;&#x5728;&#x7528;&#x6C47;&#x7F16;&#x5B9E;&#x73B0; IR&#x3002;
&#x6240;&#x4EE5;&#x6307;&#x5BFC;&#x4E66;&#x4E2D;&#xFF0C;&#x4F1A;&#x628A; IR &#x5355;&#x72EC;&#x62FF;&#x51FA;&#x6765;&#xFF0C;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x8BA8;&#x8BBA; AST &#x5982;&#x4F55;&#x7FFB;&#x8BD1;&#x5230;&#x6C47;&#x7F16;&#x3002;</p>
</blockquote>
<h2 id="&#x4ECE;-ast-&#x5230;-ir">&#x4ECE; AST &#x5230; IR</h2>
<pre><code>        &#x8BCD;&#x6CD5;&#x5206;&#x6790;           &#x8BED;&#x6CD5;&#x5206;&#x6790;           *IR&#x751F;&#x6210;*      &#x76EE;&#x6807;&#x4EE3;&#x7801;&#x751F;&#x6210;
&#x5B57;&#x8282;&#x6D41; ----------&gt; Tokens ----------&gt; &#x8BED;&#x6CD5;&#x6811; ---------&gt; IR ---------------&gt; RISC-V &#x6C47;&#x7F16;
</code></pre><p>&#x663E;&#x7136;&#xFF0C;&#x8FD9;&#x4E00;&#x6B65;&#x7684; <em>&#x8F93;&#x5165;</em> &#x662F; AST&#xFF0C; <em>&#x8F93;&#x51FA;</em> &#x662F;&#x4E00;&#x7CFB;&#x5217; IR <sup><a href="#fn_1" id="reffn_1">1</a></sup></p>
<blockquote>
<p>&#x4F8B;&#x5982;&#x524D;&#x9762;&#x7684; <code>int main(){return 0;}</code> &#x4F8B;&#x5B50;&#xFF0C;&#x8F93;&#x51FA;&#x5982; [const 0, ret]</p>
</blockquote>
<p>&#x6BCF;&#x6B65;&#x6211;&#x4EEC;&#x53EA;&#x4ECB;&#x7ECD;&#x5FC5;&#x987B;&#x7684; IR&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x80A1;&#x8111;&#x5168;&#x6574;&#x5B8C;&#x3002;
&#x5BF9;&#x4E8E;&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4E24;&#x4E2A; IR &#x6307;&#x4EE4;&#xFF1A;<code>const</code>&#x3001;<code>ret</code>&#xFF0C;&#x5982;&#x4E0B;&#x8868;&#x3002;</p>
<table>
<thead>
<tr>
<th>&#x6307;&#x4EE4;</th>
<th>&#x53C2;&#x6570;</th>
<th>&#x542B;&#x4E49;</th>
<th>IR &#x6808;&#x5927;&#x5C0F;&#x53D8;&#x5316;<sup><a href="#fn_2" id="reffn_2">2</a></sup></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>const</code></td>
<td>&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x5E38;&#x6570;</td>
<td>&#x628A;&#x4E00;&#x4E2A;&#x5E38;&#x6570;&#x538B;&#x5165;&#x6808;&#x4E2D;</td>
<td>&#x589E;&#x52A0; 1</td>
</tr>
<tr>
<td><code>ret</code></td>
<td>&#x8FD4;&#x56DE;&#x6808;&#x9876;&#x5143;&#x7D20;</td>
<td>&#x5F39;&#x51FA;&#x6808;&#x9876;&#x5143;&#x7D20;&#xFF0C;&#x5C06;&#x5176;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x51FD;&#x6570;</td>
<td>&#x51CF;&#x5C11; 1</td>
</tr>
</tbody>
</table>
<p>&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x6709;&#x5982;&#x4E0B;&#x7684;&#x5047;&#x8BBE;&#xFF1A;</p>
<ul>
<li>&#x8003;&#x8651;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x67D0;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x88AB;&#x7FFB;&#x8BD1;&#x6210;&#x4E86;&#x4E00;&#x7CFB;&#x5217; IR &#x6307;&#x4EE4;&#xFF0C;&#x90A3;&#x4E48;&#x4ECE;&#x4EFB;&#x4F55;&#x521D;&#x59CB;&#x72B6;&#x6001;&#x51FA;&#x53D1;&#x6267;&#x884C;&#x8FD9;&#x4E9B; IR &#x6307;&#x4EE4;&#xFF0C;
&#x5B8C;&#x6210;&#x540E; IR &#x6808;&#x5927;&#x5C0F;&#x589E;&#x52A0; 1&#xFF0C;&#x6808;&#x9876;&#x5C31;&#x662F;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;</li>
<li>&#x6267;&#x884C;&#x4EFB;&#x4F55;&#x8BED;&#x53E5;&#x5BF9;&#x5E94;&#x7684; IR &#x6307;&#x4EE4;&#x5E8F;&#x5217;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E; IR &#x6808;&#x5927;&#x5C0F;&#x4E0D;&#x4FBF;&#x3002;</li>
<li>&#x6267;&#x884C;&#x4EFB;&#x4F55; <code>n</code> &#x5143;&#x64CD;&#x4F5C;&#x4E4B;&#x524D;&#xFF0C;&#x6808;&#x9876;&#x7684; <code>n</code> &#x4E2A;&#x5143;&#x7D20;&#x5C31;&#x662F;&#x64CD;&#x4F5C;&#x6570;&#x3002;</li>
</ul>
<p>&#x7531;&#x6B64;&#xFF0C;step1 &#x4E2D; AST &#x7FFB;&#x8BD1;&#x5230; IR &#x5C31;&#x5F88;&#x7B80;&#x5355;&#x4E86;&#x3002;&#x53EA;&#x9700;&#x8981; Visitor &#x904D;&#x5386; AST&#xFF0C;&#x7136;&#x540E;</p>
<ol>
<li>&#x9047;&#x5230; <code>Integer(X)</code>&#xFF1A;&#x751F;&#x6210;&#x4E00;&#x6761; <code>const X</code>&#xFF0C;&#x6808;&#x5927;&#x5C0F;&#x52A0; 1&#x3002;</li>
<li>&#x9047;&#x5230; <code>Return expr ;</code>&#xFF1A;&#x5148;&#x751F;&#x6210; <code>expr</code> &#x5BF9;&#x5E94;&#x7684; IR&#xFF0C;&#x6808;&#x5927;&#x5C0F;&#x52A0; 1&#xFF1B;&#x7136;&#x540E;&#x751F;&#x6210;&#x4E00;&#x6761; <code>ret</code>&#xFF0C;&#x6808;&#x5927;&#x5C0F;&#x548C;&#x539F;&#x6765;&#x76F8;&#x540C;&#x3002;</li>
</ol>
<h2 id="ir-&#x7FFB;&#x8BD1;&#x5230;&#x6C47;&#x7F16;">IR &#x7FFB;&#x8BD1;&#x5230;&#x6C47;&#x7F16;</h2>
<pre><code>        &#x8BCD;&#x6CD5;&#x5206;&#x6790;           &#x8BED;&#x6CD5;&#x5206;&#x6790;           IR&#x751F;&#x6210;        *&#x76EE;&#x6807;&#x4EE3;&#x7801;&#x751F;&#x6210;*
&#x5B57;&#x8282;&#x6D41; ----------&gt; Tokens ----------&gt; &#x8BED;&#x6CD5;&#x6811; ---------&gt; IR ---------------&gt; RISC-V &#x6C47;&#x7F16;
</code></pre><p>&#x6808;&#x673A;&#x5668; IR &#x7FFB;&#x8BD1;&#x5230;&#x6C47;&#x7F16;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x5982;&#x4E0B;&#x8868;&#xFF0C;&#x591A;&#x6761;&#x6C47;&#x7F16;&#x6307;&#x4EE4;&#x7528;&#x5206;&#x53F7;&#x9694;&#x5F00;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>IR</th>
<th>&#x6C47;&#x7F16;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>push X</code></td>
<td><code>addi sp, sp, -4  ;  li t1, t1, X  ;  sw t1, 0(sp)</code></td>
</tr>
<tr>
<td><code>ret</code></td>
<td><code>lw t1, 0(sp)  ;  addi sp, sp, 4  ;  jr ra</code></td>
</tr>
</tbody>
</table>
<blockquote>
<p>&#x7B80;&#x8981;&#x89E3;&#x91CA;&#xFF1A;li t1 X &#x8868;&#x793A;&#x52A0;&#x8F7D;&#x7ACB;&#x5373;&#x6570; X &#x5230;&#x5BC4;&#x5B58;&#x5668; t1&#xFF1B;RISC-V &#x548C; x86 &#x4E00;&#x6837;&#x6808;&#x9876;&#x6BD4;&#x6808;&#x5E95;&#x7684;&#x5730;&#x5740;&#x4F4E;&#xFF0C;&#x6240;&#x4EE5;&#x538B;&#x6808; 4 &#x5B57;&#x8282;&#x662F;&#x6808;&#x6307;&#x9488; sp &#x51CF; 4&#x3002;
ra &#x5B58;&#x4E86;&#x8C03;&#x7528;&#x8005;&#x5730;&#x5740;&#xFF0C;jr ra &#x5C31;&#x662F;&#x5B50;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x3002;</p>
</blockquote>
<p>IR &#x6808;&#x7684;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x90FD;&#x662F; 32 &#x4F4D;&#x6574;&#x6570;&#xFF0C;&#x6240;&#x4EE5; push &#x4F7F;&#x5F97; IR &#x6808;&#x5927;&#x5C0F;&#x52A0; 1 &#x5728;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x5C31;&#x4F53;&#x73B0;&#x4E3A; <code>sp</code> &#x51CF; 4&#x3002;</p>
<p>&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x4F60;&#x5BF9;&#x4E8E; <code>int main(){return 0;}</code> &#x5E94;&#x8BE5;&#x751F;&#x6210;&#x5982;&#x4E0B;&#x6C47;&#x7F16;</p>
<pre><code>    .text
    .globl main
main:
    addi sp, sp, -4
    li t1, 233
    sw t1, 0(sp)
    lw a0, 0(sp)
    addi sp, sp, 4
    jr ra
</code></pre><h1 id="&#x4EFB;&#x52A1;">&#x4EFB;&#x52A1;</h1>
<ol>
<li>&#xFF08;&#x53EF;&#x9009;&#xFF0C;&#x63A8;&#x8350;&#xFF09;&#x6539;&#x8FDB;&#x4F60;&#x4E0A;&#x4E00;&#x6B65;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5148;&#x751F;&#x6210; IR&#xFF0C;&#x518D;&#x4ECE; IR &#x751F;&#x6210;&#x6C47;&#x7F16;&#x3002;</li>
<li>&#xFF08;&#x548C; 1. &#x4E8C;&#x9009;&#x4E00;&#xFF09;&#x6539;&#x8FDB;&#x4F60;&#x4E0A;&#x4E00;&#x6B65;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x7528;&#x6808;&#x673A;&#x5668;&#x7684;&#x601D;&#x8DEF;&#x751F;&#x6210;&#x6C47;&#x7F16;&#x3002;</li>
</ol>
<h1 id="&#x601D;&#x8003;&#x9898;">&#x601D;&#x8003;&#x9898;</h1>
<p>ANTLR</p>
<p>&#x6808;&#x673A;&#x5668;</p>
<h1 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h1>
<h1 id="&#x5907;&#x6CE8;">&#x5907;&#x6CE8;</h1>
<blockquote id="fn_1">
<sup>1</sup>. &#x5B9E;&#x9645;&#x4E0A; <code>const</code> &#x548C; <code>ret</code> &#x662F; IR &#x7684; <em>&#x6307;&#x4EE4;</em>&#x3002;&#x6211;&#x4EEC;&#x4E3A;&#x4E86;&#x7B80;&#x4FBF;&#xFF0C;&#x901A;&#x5E38;&#x76F4;&#x63A5;&#x7528; IR &#x4EE3;&#x6307; IR &#x6307;&#x4EE4;&#x3002;<a href="#reffn_1" title="Jump back to footnote [1] in the text."> &#x21A9;</a>
</blockquote>
<blockquote id="fn_2">
<sup>2</sup>. &#x6CE8;&#x610F;&#x533A;&#x5206; IR &#x6808;&#x548C;&#x6C47;&#x7F16;&#x4E2D;&#x7684;&#x6808;&#x3002;IR &#x7684;&#x6808;&#x4E2D;&#x5305;&#x542B;&#x7684;&#x5143;&#x7D20;&#x662F;&#x6574;&#x6570;&#xFF0C;IR &#x6808;&#x7684;&#x5927;&#x5C0F;&#x6307;&#x6808;&#x4E2D;&#x6709;&#x591A;&#x5C11;&#x4E2A;&#x6574;&#x6570;&#x3002;&#x5BF9;&#x4E8E; IR &#x7684;&#x6808;&#x4E0D;&#x5B58;&#x5728;&#x201C;&#x5B57;&#x8282;&#x201D;&#x8FD9;&#x4E00;&#x6982;&#x5FF5;&#x3002;<a href="#reffn_2" title="Jump back to footnote [2] in the text."> &#x21A9;</a>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part2.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 第二部分：词法语法分析工具">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第三部分：使用中间码","level":"3.2.3","depth":2,"next":{"title":"其他","level":"3.2.4","depth":2,"ref":"","articles":[{"title":"ANTLR 使用","level":"3.2.4.1","depth":3,"path":"docs/lab1alt/antlr.md","ref":"docs/lab1alt/antlr.md","articles":[]},{"title":"Visitor 模式","level":"3.2.4.2","depth":3,"path":"docs/lab1alt/visitor.md","ref":"docs/lab1alt/visitor.md","articles":[]}]},"previous":{"title":"第二部分：词法语法分析工具","level":"3.2.2","depth":2,"path":"docs/lab1alt/part2.md","ref":"docs/lab1alt/part2.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab1alt/part3.md","mtime":"2020-08-26T13:00:08.098Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-26T15:46:29.226Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

